﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.BusinessEntity.YunZhiJia;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 销售订单删除插件
    /// </summary>
    /// <remarks>
    /// 功能：销售订单删除插件
    /// 创建人：mey
    /// 创建日期：2024.7.8
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    /// 
    [Description("销售订单删除校验插件")]
    [HotUpdate]
    public class SaleOrderDeleteService : AbstractOperationServicePlugIn
    {
        //OnPreparePropertys 数据加载前，确保需要的属性被加载
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);

            //编码
            e.FieldKeys.Add("FBillNo");


            //同步CRM状态
            e.FieldKeys.Add("F_DEV_FSTATUS");

            //CRM编码
            e.FieldKeys.Add("F_DEV_CRMNumber");
            e.FieldKeys.Add("F_DEV_DataSrc");
        }
        /// <summary>
        /// 事务结束后事件
        /// </summary>
        /// <param name="e"></param>
        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            base.BeforeExecuteOperationTransaction(e);

            //读取全部的单据,for循环,转换成DynamicObject类型
            foreach (ExtendedDataEntity extended in e.SelectedRows)
            {

                DynamicObject dy = extended.DataEntity;
               

                string DataSrc = dy["F_DEV_DataSrc"].ToString();//编码

                if (DataSrc == "CRM")
                {
                    string Number = dy["F_DEV_CRMNumber"].ToString();//编码
                    if (Number != "" && Number != " ")
                    {
                        string sql = @"/*dialect*/SELECT count(*) Acount
                            FROM [CRM].[ecology].[dbo].[uf_JCH_fhtzd] where ddbh in(
                            SELECT id
                            FROM [CRM].[ecology].[dbo].[uf_JCH_ddxx] where ddbh='" + Number + "')";
                        DataTable dt = DBUtils.ExecuteDataSet(Context, sql).Tables[0];
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            if (Convert.ToInt32(dt.Rows[0][0]) > 0)
                            {
                              

                                throw new KDException("删除失败", "CRM订单已创建发货通知单，请前往CRM删除发货通知单！");
                            }
                        }
                    }


                }



            }
        }
    }
}
